package com.chang;

import com.jfinal.core.Controller;
import org.bson.Document;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class BaseController extends Controller {

    /**
     * 返回页面
     * @param PagePath
     * @param Msg
     */
    public void ReturnPage(String PagePath,String Msg)
    {
        setAttr("msg",Msg);
        render(PagePath);
    }
    /**
     * 重写getPara方法
     * @param KeyName a String specifying the name of the parameter
     * @return
     */
    public String getPara(String KeyName)
    {

        String KeyValue = this.getRequest().getParameter(KeyName);
        if(KeyValue != null)
        {
            try
            {
                KeyValue = KeyValue.trim();
                //判断字符编码是否是ISO-8859-1
                if (KeyValue.equals(new String(KeyValue.getBytes("ISO-8859-1"),"ISO-8859-1")))
                {
                    KeyValue = new String(KeyValue.getBytes("ISO-8859-1"),"utf-8");
                }

            }
            catch(Exception Exp)
            {
                System.out.println("UrlEncoded参数编码转换错误：" + Exp.toString());
            }
        }
        else
        {
            KeyValue = "";
        }
        return KeyValue;
    }
    /*******************************************************************
     * 公用方法，返回JSON
     * @param State
     * @param Msg
     * @param ReturnVal
     *******************************************************************/
    public void ReturnJSON(Object ReturnVal)
    {
        this.getResponse().setContentType("application/json");
        renderJson(ReturnVal);
    }

    /*******************************************************************
     * 公用方法，返回JSON
     * @param State 状态码
     * @param Msg 消息
     * @param ReturnVal 对象
     *******************************************************************/
    public void ReturnJSON(String State,String Msg,Object ReturnVal)
    {
        this.getResponse().setContentType("application/json");

        org.bson.Document ReturnDocment = new Document();
        ReturnDocment.put("state",State);
        ReturnDocment.put("msg",Msg);
        ReturnDocment.put("data",ReturnVal);
        renderJson(ReturnDocment);
    }

    /**
     *
     * @param State 状态码
     * @param Msg 消息
     * @param ReturnVal 对象
     * @param ItemsObject
     */
//    public void ReturnJSONForShowItems(String State,String Msg,Object ReturnVal,Object ItemsObject)
//    {
//        this.getResponse().setContentType("application/json");
//
//        org.bson.Document ReturnDocment = new Document();
//        ReturnDocment.put("state",State);
//        ReturnDocment.put("msg",Msg);
//        ReturnDocment.put("data",ReturnVal);
//        ReturnDocment.put("items",ItemsObject);
//        renderJson(ReturnDocment);
//    }

    /*******************************************************************
     * 公共方法，页面序列化为Docment
     * @param request
     * @param e
     * @return
     *******************************************************************/
    public Document FormToDocument(HttpServletRequest request, Enumeration<String> e)
    {
        org.bson.Document ReturnVal = new Document();
        while (e.hasMoreElements())
        {
            String keyString= String.valueOf(e.nextElement());

            Object valueObject = null;
            if(!keyString.equals("timeStamp") && !keyString.equals("undefined") && !keyString.equals("sn-checkbox-open-in-new-window"))
            {
                valueObject = request.getParameter(keyString);
                ReturnVal.put(keyString, valueObject);

                //System.out.println("参数：" + keyString + " 值：" + valueObject.toString());
            }
        }
        return ReturnVal;
    }
}
